#include "Window.hpp"
#include <iostream>

GLFWwindow* initWindow()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Simple Game Engine v0.0.1", NULL, NULL);

    glfwMakeContextCurrent(window);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetFramebufferSizeCallback(window, window_callback);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    initGLES();
    return window;
}
void window_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}
//void GLAPIENTRY MessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
//                                GLsizei length, const GLchar* message, const void* userParam)
//{
//    std::cout << ("GL CALLBACK: " << (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "")
//              << "type = 0x"      << type
//              << "severity = 0x"  << severity
//              << ", message = "   << message
//              << std::endl;
//}
void initGLES()
{
    gladLoadGLES2Loader((GLADloadproc)glfwGetProcAddress);

    glViewport(0, 0, WIDTH, HEIGHT);
    glClearColor(0.2f, 0.1f, 0.3f, 1.0f);

    //glEnable(GL_DEBUG_OUTPUT);
    //glDebugMessageCallback(MessageCallback, 0);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_STENCIL_TEST);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
}
